// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ImportViewModel.cs" company="Concept Architecture">
//   (c) 2010 Muhammad Umer Farooq
// </copyright>
// <summary>
//   Defines the <seealso cref="ImportViewModel" /> type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Hisaab.Client.Modules.Core.Management.ViewModel
{
    using System;
    using System.Globalization;
    using System.Windows;
    using System.Windows.Input;

    using Hisaab.Client.Common;
    using Hisaab.Client.Common.BaseClasses;

    using Microsoft.Practices.Prism;
    using Microsoft.Practices.Prism.Commands;
    using Microsoft.Practices.Prism.Regions;
    using Microsoft.Practices.ServiceLocation;
    using Microsoft.Win32;

    /// <summary>
    /// Defines the <seealso cref="ImportViewModel"/> type.
    /// </summary>
    public class ImportViewModel : ViewModelBase
    {
        #region Data Members

        /// <summary>
        /// Member variable _importBankDataCommand
        /// </summary>
        private DelegateCommand _importBankDataCommand;

        /// <summary>
        /// Member variable _selectedDataSource
        /// </summary>
        private Banks? _selectedDataSource;

        /// <summary>
        /// Member variable _browseCommand
        /// </summary>
        private ICommand _browseCommand;

        /// <summary>
        /// Member variable _filePath
        /// </summary>
        private string _filePath;

        #endregion Data Members

        #region Properties

        /// <summary>
        /// Gets or sets the bank list.
        /// </summary>
        /// <value>
        /// The bank list.
        /// </value>
        public Banks? SelectedDataSource
        {
            get
            {
                return _selectedDataSource;
            }

            set
            {
                RaisePropertyChangingEvent("SelectedDataSource");
                _selectedDataSource = value;
                RaisePropertyChangedEvent("SelectedDataSource");
            }
        }

        /// <summary>
        /// Gets or sets the file path.
        /// </summary>
        /// <value>
        /// The file path.
        /// </value>
        public string FilePath
        {
            get
            {
                return _filePath;
            }

            set
            {
                RaisePropertyChangingEvent("FilePath");
                _filePath = value;
                RaisePropertyChangedEvent("FilePath");
            }
        }

        /// <summary>
        /// Gets the browse command.
        /// </summary>
        public ICommand BrowseCommand
        {
            get
            {
                if (_browseCommand == null)
                {
                    _browseCommand = new DelegateCommand(OnBrowse);
                }

                return _browseCommand;
            }
        }

        /// <summary>
        /// Gets the export command.
        /// </summary>
        public ICommand ImportBankDataCommand
        {
            get
            {
                if (_importBankDataCommand == null)
                {
                    _importBankDataCommand = new DelegateCommand(OnBankImport, CanImport);
                }

                return _importBankDataCommand;
            }
        }

        #endregion Properties

        #region Public Methods

        /// <summary>
        /// Called when browse button clicked
        /// </summary>
        public void OnBrowse()
        {
            var dlg = new OpenFileDialog { DefaultExt = "csv", CheckPathExists = true };
            if (dlg.ShowDialog(Application.Current.MainWindow).Value)
            {
                FilePath = dlg.FileName;
            }
        }

        /// <summary>
        /// Called when bank import.
        /// </summary>
        public void OnBankImport()
        {
            if (SelectedDataSource != null)
            {
                var regionManager = ServiceLocator.Current.GetInstance<IRegionManager>();
                var query = new UriQuery
                    {
                        { "ID", ((int)SelectedDataSource).ToString(CultureInfo.InvariantCulture) },
                        { "Source", FilePath }
                    };
                var uriString = new Uri(ManagementModule.ImportBankData + query, UriKind.RelativeOrAbsolute);
                regionManager.RequestNavigate(Regions.SecondaryRegion, uriString);
            }
        }

        /// <summary>
        /// Determines whether this instance can import.
        /// </summary>
        /// <returns>
        ///   <c>true</c> if this instance can import; otherwise, <c>false</c>.
        /// </returns>
        public bool CanImport()
        {
            return !string.IsNullOrWhiteSpace(FilePath) && SelectedDataSource.HasValue;
        }

        /// <summary>
        /// Raises the PropertyChanged event.
        /// </summary>
        /// <param name="propertyName">The name of the changed property.</param>
        public override void RaisePropertyChangedEvent(string propertyName)
        {
            base.RaisePropertyChangedEvent(propertyName);
            if (_importBankDataCommand != null)
            {
                _importBankDataCommand.RaiseCanExecuteChanged();
            }
        }

        #endregion Public Methods
    }
}